/**
 * Created by Administrator on 2017/5/17.
 */

$(function(){
    index4();
});

var queryParking;
function index4(){
    cwStatistics();
    cwzyl();
    queryParkingz();
    //4
    clearInterval(queryParking);
    queryParking = setInterval(queryParkingz,60000);
    layout = "1x1";
    initPlugin();

}

//车位统计
function cwStatistics(){
    $.get( "../../cw/cwparkingspace/cwCount?type="+zytype+"&time="+zytime,function(r){
        console.log(r)
        var slist=r.cwcountlist["南区"];
        var nlist=r.cwcountlist["北区"];
        var tlist=r.cwcountlist["总额"];
        $("#soutotal").html(slist[0]);
        $("#soubsy").html(slist[1]);
        $("#soubzyl").html(slist[2]+"%");
        $("#soussy").html(slist[3]);
        $("#souszyl").html(slist[4]+"%");

        $("#nortotal").html(nlist[0]);
        $("#norbsy").html(nlist[1]);
        $("#norbzyl").html(nlist[2]+"%");
        $("#norssy").html(nlist[3]);
        $("#norszyl").html(nlist[4]+"%");


        $("#ttotal").html(tlist[0]);
        $("#tbsy").html(tlist[1]);
        $("#tbzyl").html(tlist[2]+"%");
        $("#tssy").html(tlist[3]);
        $("#tszyl").html(tlist[4]+"%");
    })
}
//车位占用率 统计图
function cwzyl(){
    var xname=null;
    if(zytype == "0"){
        xname="小时";
    }else if(zytype == "1"){
        xname="日期";
    }
    var avg=[];
    /*var a=100/3;
        console.log(a.toFixed(1))*/
    $.get( "../../cw/cwparkingspace/cwStatistics?type="+zytype+"&time="+zytime,function(r){
        var soulist=r.southlist;
        var norlist=r.northlist;
        for (var i = 0; i <soulist[1].length ; i++) {
            avg.push(((parseFloat(soulist[1][i])+parseFloat(norlist[1][i]))/2).toFixed(1))
        }
        var myChart = echarts.init($("#zhanyonglv")[0]);
        option = {
            tooltip : {
                trigger: 'axis',
                formatter: function(params) { //自定义函数修改折线图给数据加单位
                    var str = '';//声明一个变量用来存储数据
                    str += '<div>'+ params[0].name +'</div>'; //显示首行的函数
                    for(var i=0; i<params.length; i++) { //图显示的数据较多时需要循环数据，这样才可以成功的给多条数据添加单位
                        str += '<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:'+params[i].color+';"></span>'+ params[i].seriesName +'</span> : <span>'+ params[i].data+'%' +'</br>';
                    }
                    return str;
                }
            },
            grid: {
                x: 46,
                y:30,
                x2:30,
                y2:20,
                borderWidth: 0
            },

            calculable : false,
            legend: {
                data:['南区','北区','平均值',],
                textStyle:{
                    color:"#e9ebee"

                }
            },
            xAxis : [
                {
                    type : 'category',
                    name:xname,
                    data : soulist[0],
                    splitLine: {
                        show: false
                    },
                    axisLabel: {
                        show: true,
                        textStyle: {
                            color: '#a4a7ab',
                            align: 'center'
                        }
                    }

                }
            ],
            yAxis : [
                {
                    type : 'value',
                    name : '占用率',
                    max : 100 ,
                    axisLabel : {
                        formatter: '{value} %',
                        textStyle: {
                            color: '#a4a7ab',
                        }
                    },
                    splitLine: {
                        show: false
                    },
                }
            ],
            series : [

                {
                    name:'南区',
                    type:'bar',
                    data:soulist[1],
                    itemStyle: {
                        normal: {
                            color:"#0ad5ff"
                        }
                    }
                },
                {
                    name:'北区',
                    type:'bar',
                    data:norlist[1],
                    itemStyle: {
                        normal: {
                            color:"#005ea1"
                        }
                    }
                },
                {
                    name:'平均值',
                    type:'line',
                    data:avg,
                    itemStyle: {
                        normal: {
                            color:"#1afffd"
                        }
                    }
                }
            ]
        };
        myChart.setOption(option);
        window.addEventListener("resize",function(){
            myChart.resize();
        });
    })
}

//查询剩余车位
function queryParkingz(){
    $.get(baseURL + "cw/cwparkingspace/spaceShow", function(r){
        $("#ln").html(r.p1);
        $("#ftn").html(r.p2);
        $("#fbn").html(r.p3);
        $("#btn").html(r.p4);
        $("#fcn").html(r.p5);
        $("#ls").html(r.p6);
        $("#fbs").html(r.p7);
        $("#bts").html(r.p9);
        $("#fcs").html(r.p10);

    });
}
//时间选择
var zytype="0";
var zytime="";
function choicetime(data){
    if(data =="0"){
        zytype="0";
        $("#choiceday").css("display","inline-block");
        $("#choiceday").val("");
        $("#choicemonth").val("");
        zytime ="";
        $("#thisDay").css("background-color","#3366FF");
        $('#thisMonth').css("background-color","");
        $("#choicemonth").css("display","none");
        cwStatistics();
        cwzyl();
    }else if(data =="1"){
        zytype="1";
        $("#choicemonth").css("display","inline-block");
        $("#choiceday").val("");
        $("#choicemonth").val("");
        zytime ="";
        $("#thisMonth").css("background-color","#3366FF");
        $('#thisDay').css("background-color","");
        $("#choiceday").css("display","none");
        cwStatistics();
        cwzyl();
    }
}
//选择时间后 点击查询事件
function query(){
    //获取input框的值
    if(zytype =="0"){
        zytime =$("#choiceday").val();
    }else if(zytype=="1"){
        zytime =$("#choicemonth").val();
    }
    cwStatistics();
    cwzyl();
}

function startPreview3(cameraIndexCode){
    var name =$(".camera-run").attr("name");
    if(name=="camera-left"){
        $(".camera-run").attr("src","../../statics/css/img/camera-left.png");
        $(".camera-run").removeClass("camera-run");
    }else {
        $(".camera-run").attr("src","../../statics/css/img/camera-right.png");
        $(".camera-run").removeClass("camera-run");
    }
    var name1 =$("#"+cameraIndexCode).attr("name");

    if(name1=="camera-left"){
        $("#"+cameraIndexCode).attr("src","../../statics/css/img/camera-left1.png");
        $("#"+cameraIndexCode).addClass("camera-run");
    }else {
        $("#"+cameraIndexCode).attr("src","../../statics/css/img/camera-right1.png");
        $("#"+cameraIndexCode).addClass("camera-run");
    }
    var streamMode = 0;                                     //主子码流标识：0-主码流，1-子码流
    var transMode = 1;                                      //传输协议：0-UDP，1-TCP
    var gpuMode = 0;                                        //是否启用GPU硬解，0-不启用，1-启用
    cameraIndexCode = cameraIndexCode.replace(/(^\s*)/g, "");
    cameraIndexCode = cameraIndexCode.replace(/(\s*$)/g, "");
    oWebControl.JS_RequestInterface({
        funcName: "startPreview",
        argument: JSON.stringify({
            cameraIndexCode:cameraIndexCode,                //监控点编号
            streamMode: streamMode,                         //主子码流标识
            transMode: transMode,                           //传输协议
            gpuMode: gpuMode,                               //是否开启GPU硬解
            wndId:-1                                     //可指定播放窗口
        })
    })
}
//视频插件加载完后运行的方法
function videoLoad() {
    startPreview3('e30c9bc3682e43dfb61af1ef21faa7cb',-1);
}
